package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Vegetation;
import com.fsnbigdata.domain.QVegetation;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = VegetationRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = VegetationRepositoryCustom.class)
@Transactional(readOnly = true)
public class VegetationRepositoryImpl extends QueryDslRepositorySupportExt<Vegetation> implements VegetationRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    VegetationRepositoryImpl() {
        super(Vegetation.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_C_NAME = "edificationCName";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String HABITAT_AGROTYPE = "habitatAgrotype";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_C_LATIN = "edificationCLatin";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String HABITAT_TEMPERATURE = "habitatTemperature";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_A_ORGANISM_ID = "edificationAOrganismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COMMUNITY_CHARACTERISTICS = "communityCharacteristics";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_A_NAME = "edificationAName";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_B_ORGANISM_ID = "edificationBOrganismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY = "category";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_1 = "categoryA1";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_2 = "categoryA2";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_B_1 = "categoryB1";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_C_ORGANISM_ID = "edificationCOrganismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_5 = "categoryA5";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_B_4 = "categoryB4";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_6 = "categoryA6";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String HABITAT_PRECIPITATION = "habitatPrecipitation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_3 = "categoryA3";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_B_2 = "categoryB2";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_A_4 = "categoryA4";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY_B_3 = "categoryB3";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String HABITAT_ALTITUDE = "habitatAltitude";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_A_LATIN = "edificationALatin";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_B_NAME = "edificationBName";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EDIFICATION_B_LATIN = "edificationBLatin";


    @Override
    public Page<Vegetation> findAll(Predicate predicate, Pageable pageable) {
        QVegetation vegetation = QVegetation.vegetation;
        JPQLQuery<Vegetation> query = from(vegetation).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, vegetation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Vegetation> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QVegetation vegetation = QVegetation.vegetation;
        JPQLQuery<Vegetation> query = from(vegetation);
        if(creator != null ){
            query.where(vegetation.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { vegetation.name, vegetation.edificationAName, vegetation.edificationALatin, vegetation.edificationAOrganismId, vegetation.edificationBName, vegetation.edificationBLatin, vegetation.edificationBOrganismId, vegetation.edificationCName, vegetation.edificationCLatin, vegetation.edificationCOrganismId, vegetation.category, vegetation.categoryA1, vegetation.categoryA2, vegetation.categoryA3, vegetation.categoryA4, vegetation.categoryA5, vegetation.categoryA6, vegetation.categoryB1, vegetation.categoryB2, vegetation.categoryB3, vegetation.categoryB4, vegetation.habitatAltitude, vegetation.habitatAgrotype, vegetation.habitatTemperature, vegetation.habitatPrecipitation, vegetation.communityCharacteristics, vegetation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, vegetation.name).map(EDIFICATION_A_NAME, vegetation.edificationAName).map(EDIFICATION_A_LATIN, vegetation.edificationALatin).map(EDIFICATION_A_ORGANISM_ID, vegetation.edificationAOrganismId).map(EDIFICATION_B_NAME, vegetation.edificationBName).map(EDIFICATION_B_LATIN, vegetation.edificationBLatin).map(EDIFICATION_B_ORGANISM_ID, vegetation.edificationBOrganismId).map(EDIFICATION_C_NAME, vegetation.edificationCName).map(EDIFICATION_C_LATIN, vegetation.edificationCLatin).map(EDIFICATION_C_ORGANISM_ID, vegetation.edificationCOrganismId).map(CATEGORY, vegetation.category).map(CATEGORY_A_1, vegetation.categoryA1).map(CATEGORY_A_2, vegetation.categoryA2).map(CATEGORY_A_3, vegetation.categoryA3).map(CATEGORY_A_4, vegetation.categoryA4).map(CATEGORY_A_5, vegetation.categoryA5).map(CATEGORY_A_6, vegetation.categoryA6).map(CATEGORY_B_1, vegetation.categoryB1).map(CATEGORY_B_2, vegetation.categoryB2).map(CATEGORY_B_3, vegetation.categoryB3).map(CATEGORY_B_4, vegetation.categoryB4).map(HABITAT_ALTITUDE, vegetation.habitatAltitude).map(HABITAT_AGROTYPE, vegetation.habitatAgrotype).map(HABITAT_TEMPERATURE, vegetation.habitatTemperature).map(HABITAT_PRECIPITATION, vegetation.habitatPrecipitation).map(COMMUNITY_CHARACTERISTICS, vegetation.communityCharacteristics).map(CREATOR, vegetation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, vegetation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Vegetation> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QVegetation vegetation = QVegetation.vegetation;
        JPQLQuery<Vegetation> query = from(vegetation);
        Path<?>[] paths = new Path<?>[] { vegetation.name, vegetation.edificationAName, vegetation.edificationALatin, vegetation.edificationAOrganismId, vegetation.edificationBName, vegetation.edificationBLatin, vegetation.edificationBOrganismId, vegetation.edificationCName, vegetation.edificationCLatin, vegetation.edificationCOrganismId, vegetation.category, vegetation.categoryA1, vegetation.categoryA2, vegetation.categoryA3, vegetation.categoryA4, vegetation.categoryA5, vegetation.categoryA6, vegetation.categoryB1, vegetation.categoryB2, vegetation.categoryB3, vegetation.categoryB4, vegetation.habitatAltitude, vegetation.habitatAgrotype, vegetation.habitatTemperature, vegetation.habitatPrecipitation, vegetation.communityCharacteristics, vegetation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(vegetation.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, vegetation.name).map(EDIFICATION_A_NAME, vegetation.edificationAName).map(EDIFICATION_A_LATIN, vegetation.edificationALatin).map(EDIFICATION_A_ORGANISM_ID, vegetation.edificationAOrganismId).map(EDIFICATION_B_NAME, vegetation.edificationBName).map(EDIFICATION_B_LATIN, vegetation.edificationBLatin).map(EDIFICATION_B_ORGANISM_ID, vegetation.edificationBOrganismId).map(EDIFICATION_C_NAME, vegetation.edificationCName).map(EDIFICATION_C_LATIN, vegetation.edificationCLatin).map(EDIFICATION_C_ORGANISM_ID, vegetation.edificationCOrganismId).map(CATEGORY, vegetation.category).map(CATEGORY_A_1, vegetation.categoryA1).map(CATEGORY_A_2, vegetation.categoryA2).map(CATEGORY_A_3, vegetation.categoryA3).map(CATEGORY_A_4, vegetation.categoryA4).map(CATEGORY_A_5, vegetation.categoryA5).map(CATEGORY_A_6, vegetation.categoryA6).map(CATEGORY_B_1, vegetation.categoryB1).map(CATEGORY_B_2, vegetation.categoryB2).map(CATEGORY_B_3, vegetation.categoryB3).map(CATEGORY_B_4, vegetation.categoryB4).map(HABITAT_ALTITUDE, vegetation.habitatAltitude).map(HABITAT_AGROTYPE, vegetation.habitatAgrotype).map(HABITAT_TEMPERATURE, vegetation.habitatTemperature).map(HABITAT_PRECIPITATION, vegetation.habitatPrecipitation).map(COMMUNITY_CHARACTERISTICS, vegetation.communityCharacteristics).map(CREATOR, vegetation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, vegetation);
    }
}
